<template>
	<view>
		<!-- 头部 -->
		<view class="header">
			<icon type="search" size="16" color="#bbb">
			</icon>
			<input type="text" v-model="query" confirm-type="search" @confirm="goodsSearch">
		</view>

		<!-- 过滤菜单 -->
		<view class="filter-menu">
			<view @click="selectMenu(index)" :class="{active:activeIndex === index}" v-for="(item, index) in menuList" :key="index">{{item}}</li>
			</view>
		</view>


		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods" v-for="(item, index) in goodsList" :key="index">
				<image :src="item.goods_small_logo" alt="">
					<view class="right">
						<view class="goods-name text-line2">{{item.goods_name}}</view>
						<view class="price">￥<text>{{item.goods_price}}</text>.00</view>
					</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				activeIndex: 0,
				menuList: ['综合', '销量', '价格'],
				// 传递过来的参数  搜索关键词
				query: '',
				// 商品列表数据
				goodsList: [],
				// 页码
				pagenum:1
			}
		},
		methods: {
			// 菜单的选择
			selectMenu(index) {
				this.activeIndex = index
			},
			async goodsSearch() {
				var res = await this.$request({
					url: '/v1/goods/search',
					data: {
						query: this.query,
						pagenum: this.pagenum,
						pagesize: 6
					}
				})
				console.log(res)
				this.goodsList = res.goods
			}
		},
		onLoad(options) {
			// 接收url传递过来的参数
			var query = options.catName
			this.query = query
			console.log(query)
			// 根据参数发送请求
			this.goodsSearch()
		},
		// 下拉加载钩子
		onPullDownRefresh() {
			// console.log('xialai')
			// 重新加载第一页数据
			this.pagenum = 1
			this.goodsSearch()
		},
		// 上拉加载钩子
		onReachBottom(){
			console.log('上拉加载')
			// 发送请求 page页码+1
			this.pagenum++
			this.goodsSearch()
		}
	}
</script>

<style lang="less">
	.header {
		height: 120rpx;
		padding: 30rpx 16rpx;
		box-sizing: border-box;
		background-color: #eee;
		position: relative;

		icon {
			position: absolute;
			top: 48rpx;
			left: 44rpx;
		}

		input {
			height: 60rpx;
			width: 100%;
			border-radius: 4rpx;
			background-color: #fff;
			padding-left: 80rpx;
			box-sizing: border-box;
		}
	}

	.top-header {
		position: sticky;
		top: 0;
		left: 0;
		right: 0;
		background-color: #fff;
	}

	.header {
		height: 120rpx;
		padding: 30rpx 16rpx;
		box-sizing: border-box;
		background-color: #eee;
		position: relative;

		icon {
			position: absolute;
			top: 48rpx;
			left: 44rpx;
		}

		input {
			height: 60rpx;
			width: 100%;
			border-radius: 4rpx;
			background-color: #fff;
			padding-left: 80rpx;
			box-sizing: border-box;
		}
	}

	.filter-menu {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 100rpx;

		view.active {
			color: #EB4450;
		}
	}

	.goods {
		border-top: 1rpx solid #ddd;
		height: 260rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
		display: flex;
		align-items: center;

		image {
			width: 200rpx;
			height: 200rpx;
		}

		.right {
			flex: 1;
			margin-left: 26rpx;

			.price {
				color: #eb4450;
				font-size: 24rpx;
				margin-top: 80rpx;

				text {
					font-size: 36rpx;
				}
			}
		}
	}

	.btm-line {
		text-align: center;
	}
</style>
